package top.wilsonlv.jaguar.cloud.upms.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;
import top.wilsonlv.jaguar.basecrud.BaseController;
import top.wilsonlv.jaguar.cloud.upms.controller.vo.UserDeviceVO;
import top.wilsonlv.jaguar.cloud.upms.entity.UserDevice;
import top.wilsonlv.jaguar.cloud.upms.mapper.UserDeviceMapper;
import top.wilsonlv.jaguar.cloud.upms.sdk.dto.DeviceBindDTO;
import top.wilsonlv.jaguar.cloud.upms.service.DeviceService;
import top.wilsonlv.jaguar.cloud.upms.service.UserDeviceService;
import top.wilsonlv.jaguar.commons.web.response.JsonResult;
import top.wilsonlv.jaguar.commons.web.util.WebUtil;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 用户设备  前端控制器
 * </p>
 *
 * @author lvws
 * @since 2022-01-13
 */
@Validated
@RestController
@RequestMapping("/userDevice")
@Api(tags = "用户设备管理")
@RequiredArgsConstructor
public class UserDeviceController extends BaseController<UserDevice, UserDeviceMapper, UserDeviceService> {

    private final DeviceService deviceService;

    @ApiOperation(value = "分页查询用户设备")
    @PreAuthorize("hasAuthority('用户设备管理')")
    @GetMapping(value = "/page")
    public JsonResult<Page<UserDeviceVO>> page(@ApiIgnore Page<UserDevice> page,
                                               @ApiParam("用户ID") @RequestParam(required = false) Long userId,
                                               @ApiParam("终端设备UID") @RequestParam(required = false) String deviceUid) {
        Map<String, Object> params = new HashMap<>(2);
        params.put("userId", userId);
        params.put("deviceUid", deviceUid);
        return success(service.queryUserDevice(page, params));
    }

    @ApiOperation(value = "分页查询用户设备，右连接user表")
    @PreAuthorize("hasAuthority('用户设备管理')")
    @GetMapping(value = "/rightJoinUserPage")
    public JsonResult<Page<UserDeviceVO>> rightJoinUserPage(@ApiIgnore Page<UserDevice> page,
                                                            @ApiParam("终端设备UID") String deviceUid,
                                                            @ApiParam("用户ID") @RequestParam(required = false) Long userId) {
        Map<String, Object> params = new HashMap<>(2);
        params.put("userId", userId);
        params.put("deviceUid", deviceUid);
        return success(service.rightJoinUserPage(page, params));
    }

    @ApiOperation(value = "用户设备详情")
    @PreAuthorize("hasAuthority('用户设备管理')")
    @GetMapping(value = "/{id}")
    public JsonResult<UserDeviceVO> detail(@PathVariable Long id) {
        return success(service.getDetail(id));
    }

    @ApiOperation(value = "绑定设备")
    @PreAuthorize("hasAuthority('用户设备管理')")
    @PostMapping("/bindDevice")
    public JsonResult<Void> bindDevice(@Valid @RequestBody DeviceBindDTO bindDTO,
                                       @RequestParam Long userId,
                                       @RequestParam(required = false) String sign,
                                       @RequestParam(required = false) String nonce,
                                       @RequestParam(required = false) Long timestamp,
                                       HttpServletRequest request) {

        deviceService.bindDevice(userId, WebUtil.getHost(), bindDTO,
                timestamp, nonce, sign, request.getParameterMap());
        return success();
    }

    @ApiOperation(value = "解绑设备")
    @PreAuthorize("hasAuthority('用户设备管理')")
    @DeleteMapping("/unbindDevice")
    public JsonResult<Void> unbindDevice(@ApiParam("用户ID") @RequestParam Long userId,
                                         @ApiParam("设备ID") @RequestParam String deviceUid) {
        service.unbindDevice(userId, deviceUid);
        return success();
    }
}